package com.stenio.fancat.service.shop;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.domain.PageList;
import com.stenio.fancat.domain.baby.BabyAccount;
import com.stenio.fancat.domain.baby.BabyRelationship;
import com.stenio.fancat.domain.behavior.BehaviorOperationImage;
import com.stenio.fancat.form.baby.BabyAccountForm;
import com.stenio.fancat.mapper.Behavior.BehaviorLogMapper;
import com.stenio.fancat.mapper.Behavior.BehaviorOperationImageMapper;
import com.stenio.fancat.mapper.baby.BabyAccountMapper;
import com.stenio.fancat.mapper.baby.BabyRelationshipMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuan
 * @create 2019-02-13 11:52
 */
@Service
public class PhotoService {

    @Autowired
    private BabyAccountMapper babyAccountMapper;

    @Autowired
    private BabyRelationshipMapper babyRelationshipMapper;

    @Autowired
    private BehaviorLogMapper behaviorLogMapper;

    @Autowired
    private BehaviorOperationImageMapper behaviorOperationImageMapper;

    public Page<BabyAccount> selectBabyInfo(BabyAccountForm babyAccountForm) {
        Page<BabyAccount> page = new Page<BabyAccount>(babyAccountForm.getPageNo(), babyAccountForm.getPageSize());
        page.setRecords(babyAccountMapper.selectBabyInfo(page));
        return page;
    }

    public PageList getByBabyId(String id, Integer pageNo, Integer pageSize) {
        PageList page = new PageList();
        List list = selectOperationImages(id);
        page.setCurrentPage(pageNo);
        //设置每页数据为十条
        page.setPageSize(12);
        //每页的开始数
        page.setStar((page.getCurrentPage() - 1) * page.getPageSize());
        //list的大小
        int count = list.size();
        //设置总页数
        page.setTotalPage(count);
        //对list进行截取
        page.setDataList(list.subList(page.getStar(),count-page.getStar()>page.getPageSize()?page.getStar()+page.getPageSize():count));
        //设置作用域
        return page;
    }



    public List selectOperationImages(String babyId) {
        List<BabyRelationship> babyRelationships = babyRelationshipMapper.selectUserListByBabyId(babyId);
        //List<List<BehaviorOperationImage>> b = new ArrayList<>();
        List<BehaviorOperationImage> b = new ArrayList<>();
        for(BabyRelationship babyUser : babyRelationships) {
            List<String> operationIds = behaviorLogMapper.selectOperationIds(babyUser.getUserId());
            for(String operationId : operationIds) {
                List<BehaviorOperationImage> behaviorOperationImages = behaviorOperationImageMapper.selectBehaviorOperationImages(operationId);
                for(BehaviorOperationImage bb : behaviorOperationImages) {
                    b.add(bb);
                }
                //b.add(behaviorOperationImages);
            }
        }
        return b;
    }

}
